package com.example.Nurse_Notifier;

import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class StringItemAdapter extends ArrayAdapter<XbeeDevice> {

    private Context context;
    XbeeDeviceContainer xbeeDeviceContainer = null;
    Typeface typefaceRegular;
    Typeface typefaceBold;
    int currentItem = 0;

    static class ViewHolder {
        ImageView arrowImageView;
        ImageView listImageView;
        TextView deviceIdTV;
        TextView deviceStatusTV;
    }

    public StringItemAdapter(Context context, int textViewResourceId,
                             XbeeDeviceContainer xbeeDeviceContainer) {
        super(context, textViewResourceId, xbeeDeviceContainer);
        this.context = context;
        this.xbeeDeviceContainer = xbeeDeviceContainer;
        typefaceRegular = Typeface.createFromAsset(context.getAssets(), "Roboto-Regular.ttf");
        typefaceBold = Typeface.createFromAsset(context.getAssets(), "Roboto-Bold.ttf");
    }

    public int getCurrentItem() {
        return currentItem;
    }

    public void setCurrentItem(int currentItem) {
        this.currentItem = currentItem;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        return createView(position, convertView);
    }

    public View createView(final int position, View convertView) {
        LayoutInflater inflater = (LayoutInflater) context.
                getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = convertView;
        ViewHolder holder;
        if (convertView == null) {
            v = inflater.inflate(R.layout.list_row, null);
            holder = new ViewHolder();
            holder.arrowImageView = (ImageView) v.findViewById(R.id.arrowImg);
            holder.listImageView = (ImageView) v.findViewById(R.id.list_image);
            holder.deviceIdTV = (TextView) v.findViewById(R.id.tvCity);
            holder.deviceStatusTV = (TextView) v.findViewById(R.id.tvCondition);
            holder.deviceIdTV.setTypeface(typefaceBold);
            holder.deviceStatusTV.setTypeface(typefaceRegular);
            v.setTag(holder);

        } else {
            holder = (ViewHolder) v.getTag();
        }
        holder.listImageView.setImageDrawable(xbeeDeviceContainer.get(position).getIconDrawable());
        holder.deviceIdTV.setText(xbeeDeviceContainer.get(position).getMainText());
        holder.deviceIdTV.setTextColor(xbeeDeviceContainer.get(position).getCurTextColorPrimary());
        holder.deviceStatusTV.setText(xbeeDeviceContainer.get(position).getStatusText());
        holder.deviceStatusTV.setTextColor(xbeeDeviceContainer.get(position).getCurTextColorSecond());
        v.setBackgroundColor(xbeeDeviceContainer.get(position).getCurColor());
        return v;
    }
}
